# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import frmWin, msg, button, frmLine
import gettext
_ = gettext.gettext

def handleOption(ui, i):
	if i[0] <> -2:
		ui.options_win = 0		
		if i[0] <> -1:
			ui.options_index = -1
			ui.options_list = 0			
		ui.refreshScr()
		del ui.winOpt2
		# ACCEPT
		if not i[0]:
			if ui.mam.config['show_groups']:
				found = 0
				for g in ui.mam.contactListManager.gList.grupos:
					for p in g.passports.passports:
						if p.name == i[1][0].text:
							found = 1
							# If I did not change the nick
							# if nick == realnick
							if i[1][2].text == i[1][1].text:
								break
							# If I want to come back to realnick
							elif i[1][2].text == p.name:
								p.set_nick(p.realnick)
								try:
									del ui.mam.aliasesList[p.name]
								except:
									pass
								ui.mam.m.userren(p.name, p.realnick)
							# If I renamed the nick
							else:
								p.set_nick(i[1][2].text)
								ui.mam.aliasesList[p.name] = i[1][2].text
							break
					if found:
						break
			else:
				for p in mam.ui.mam.contactListManager.pList.passports:
					if p.name == i[1][0].text:
						# If I did not change the nick
						if i[1][2].text == i[1][1].text:
							break
						# If I want to come back to realnick
						elif i[1][2].text == p.name:
							p.set_nick(p.realnick)
							del ui.mam.aliasesList[p.name]
							mam.m.userren(p.name, p.realnick)
						# If I renamed the nick
						else:
							p.set_nick(i[1][2].text)
							ui.mam.aliasesList[p.name] = i[1][2].text
						break
			ui.mam.dumpAliaseslist('.aliases')

def showWin(mam):
		if int(mam.config['show_groups']) == 1:
			passport = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].name
			rnick = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].realnick
			nick = mam.contactListManager.gList.grupos[mam.contactListManager.default_group].passports.passports[mam.contactListManager.default_user].nick
		else:
			passport = mam.contactListManager.pList.passports[mam.contactListManager.default_user].name
			rnick = mam.contactListManager.pList.passports[mam.contactListManager.default_user].realnick
			nick = mam.contactListManager.pList.passports[mam.contactListManager.default_user].nick
		mam.ui.winOpt2 = frmWin.FrmWin(mam, _("USER RENAME"), \
		[msg.Msg(_("Leave nick same as passport to keep realnick") + '.', '', 1)], \
		[button.Button(_("ACCEPT"), 1), button.Button(_("CANCEL"), 0)], mam.ui.maxX * 5 / 6, \
		[frmLine.FrmLine(_("Passport"), passport, 0, 0), \
		frmLine.FrmLine(_("Real Nick"), rnick, 0, 0), \
		frmLine.FrmLine(_("Nick"), nick, 1, 1)])
